package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TRisk;
import interfaces.IRiskService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class RiskAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IRiskService riskService;

	private TRisk riskBean;
	private List<TRisk> riskList;

	private String rskNo;
	private String rskNos;
	private String merNo;
	private String rskTarget;
	private String rskAddition;
	private String rskCondition;
	private String rskScope;
	private String rskPay;
	private Double rskDay;
	private Double rskCount;
	private String rskPolicy;
	private String rskCode;
	private String rskMessage;
	private String rskState;
	private Date rskDate;
	private Date rskDateTop;
	private Date rskDateEnd;
	private String rskRemark;

	// 展示对象
	public String show() throws Exception {
		if (!riskService.exist(rskNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			riskBean = riskService.fetch(rskNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setRskCode("R101");
		riskBean.setRskMessage("High Risk");
		riskBean.setRskCount(1D);
		riskBean.setRskDay(1D);
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setMerNo(merNo);
		riskBean.setRskTarget(rskTarget.replaceAll(" ", "").replaceAll(",", "\\|"));
		riskBean.setRskAddition(rskAddition);
		riskBean.setRskCondition(rskCondition);
		riskBean.setRskScope(rskScope);
		riskBean.setRskPay(rskPay);
		riskBean.setRskDay(rskDay);
		riskBean.setRskCount(rskCount);
		riskBean.setRskPolicy(rskPolicy);
		riskBean.setRskCode(rskCode);
		riskBean.setRskMessage(rskMessage);
		riskBean.setRskState(rskState);
		riskBean.setRskDate(new Date());
		riskBean.setRskRemark(rskRemark);
		riskService.persist(riskBean);
		return SUCCESS;
	}
	
	public String savedefault() throws Exception {
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setMerNo(merNo);
		riskBean.setRskTarget("持卡人IP|持卡人卡号|持卡人邮箱|持卡人电话|持卡人地址");
		riskBean.setRskAddition("否");
		riskBean.setRskCondition("IP次数");
		riskBean.setRskScope("商户范围");
		riskBean.setRskPay("成功交易");
		riskBean.setRskDay(10D);
		riskBean.setRskCount(1D);
		riskBean.setRskPolicy("警告");
		riskBean.setRskCode("R001");
		riskBean.setRskMessage("Hisk Risk");
		riskBean.setRskState("1");
		riskBean.setRskDate(new Date());
		riskBean.setRskRemark("同持卡人10天内重复成功交易警告");
		riskService.persist(riskBean);
		
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setMerNo(merNo);
		riskBean.setRskTarget("持卡人IP|持卡人卡号|持卡人邮箱|持卡人电话|持卡人地址");
		riskBean.setRskAddition("否");
		riskBean.setRskCondition("IP次数");
		riskBean.setRskScope("网站范围");
		riskBean.setRskPay("全部交易");
		riskBean.setRskDay(1D);
		riskBean.setRskCount(5D);
		riskBean.setRskPolicy("警告");
		riskBean.setRskCode("R002");
		riskBean.setRskMessage("Hisk Risk");
		riskBean.setRskState("1");
		riskBean.setRskDate(new Date());
		riskBean.setRskRemark("同持卡人同网站1天内重复提交5次或以上警告");
		riskService.persist(riskBean);
		
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setMerNo(merNo);
		riskBean.setRskTarget("持卡人卡号|持卡人邮箱|持卡人电话|持卡人地址");
		riskBean.setRskAddition("是");
		riskBean.setRskCondition("IP次数");
		riskBean.setRskScope("商户范围");
		riskBean.setRskPay("全部交易");
		riskBean.setRskDay(180D);
		riskBean.setRskCount(3D);
		riskBean.setRskPolicy("警告");
		riskBean.setRskCode("R003");
		riskBean.setRskMessage("Hisk Risk");
		riskBean.setRskState("1");
		riskBean.setRskDate(new Date());
		riskBean.setRskRemark("同持卡人180天内更换IP支付3次或以上警告");
		riskService.persist(riskBean);
		
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setMerNo(merNo);
		riskBean.setRskTarget("持卡人IP|持卡人邮箱|持卡人电话|持卡人地址");
		riskBean.setRskAddition("是");
		riskBean.setRskCondition("卡号次数");
		riskBean.setRskScope("商户范围");
		riskBean.setRskPay("全部交易");
		riskBean.setRskDay(180D);
		riskBean.setRskCount(3D);
		riskBean.setRskPolicy("警告");
		riskBean.setRskCode("R004");
		riskBean.setRskMessage("Hisk Risk");
		riskBean.setRskState("1");
		riskBean.setRskDate(new Date());
		riskBean.setRskRemark("同持卡人180天内更换卡号支付3次或以上警告");
		riskService.persist(riskBean);
		
		riskBean = new TRisk();
		riskBean.setRskNo(StringUtil.guid());
		riskBean.setMerNo(merNo);
		riskBean.setRskTarget("持卡人IP|持卡人卡号|持卡人电话|持卡人地址");
		riskBean.setRskAddition("是");
		riskBean.setRskCondition("邮箱次数");
		riskBean.setRskScope("商户范围");
		riskBean.setRskPay("全部交易");
		riskBean.setRskDay(180D);
		riskBean.setRskCount(3D);
		riskBean.setRskPolicy("警告");
		riskBean.setRskCode("R005");
		riskBean.setRskMessage("Hisk Risk");
		riskBean.setRskState("1");
		riskBean.setRskDate(new Date());
		riskBean.setRskRemark("同持卡人180天内更换邮箱支付3次或以上警告");
		riskService.persist(riskBean);
		return SUCCESS;
	}
	
	// 持久化对象
	public String move() throws Exception {
		riskService.remove(rskNo);
		return SUCCESS;
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("rskNo", StringUtil.split(rskNo));
		paramMap.put("merNo", merNo);
		paramMap.put("rskTarget", rskTarget);
		paramMap.put("rskAddition", rskAddition);
		paramMap.put("rskCondition", rskCondition);
		paramMap.put("rskScope", rskScope);
		paramMap.put("rskPay", rskPay);
		paramMap.put("rskDay", rskDay);
		paramMap.put("rskCount", rskCount);
		paramMap.put("rskPolicy", rskPolicy);
		paramMap.put("rskCode", rskCode);
		paramMap.put("rskMessage", rskMessage);
		paramMap.put("rskState", rskState);
		paramMap.put("rskDate", ParameterUtil.dateCombin(rskDateTop, rskDateEnd));
		paramMap.put("rskRemark", rskRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "rskDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TRisk> resultSet = riskService.search(querySet);
		riskList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("rskNo", StringUtil.split(rskNo));
		paramMap.put("merNo", merNo);
		paramMap.put("rskTarget", rskTarget);
		paramMap.put("rskAddition", rskAddition);
		paramMap.put("rskCondition", rskCondition);
		paramMap.put("rskScope", rskScope);
		paramMap.put("rskPay", rskPay);
		paramMap.put("rskDay", rskDay);
		paramMap.put("rskCount", rskCount);
		paramMap.put("rskPolicy", rskPolicy);
		paramMap.put("rskCode", rskCode);
		paramMap.put("rskMessage", rskMessage);
		paramMap.put("rskState", rskState);
		paramMap.put("rskDate", ParameterUtil.dateCombin(rskDateTop, rskDateEnd));
		paramMap.put("rskRemark", rskRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "rskDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TRisk> resultSet = riskService.search(querySet);
		riskList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("商户编号");
		fieldHead.add("风险目标");
		fieldHead.add("是否附加");
		fieldHead.add("附加条件");
		fieldHead.add("交易范围");
		fieldHead.add("交易状态");
		fieldHead.add("交易天数");
		fieldHead.add("风险次数");
		fieldHead.add("风险策略");
		fieldHead.add("风险代码");
		fieldHead.add("风险描述");
		fieldHead.add("是否可用");
		fieldHead.add("创建日期日期");
		fieldHead.add("创建日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TRisk riskBean : riskList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(riskBean.getRskNo());
			fieldItem.add(riskBean.getMerNo());
			fieldItem.add(riskBean.getRskTarget());
			fieldItem.add((riskBean.getRskAddition().equals("0")) ? "无效" : "有效");
			fieldItem.add(riskBean.getRskCondition());
			fieldItem.add(riskBean.getRskScope());
			fieldItem.add(riskBean.getRskPay());
			fieldItem.add(riskBean.getRskDay().toString());
			fieldItem.add(riskBean.getRskCount().toString());
			fieldItem.add(riskBean.getRskPolicy());
			fieldItem.add(riskBean.getRskCode());
			fieldItem.add(riskBean.getRskMessage());
			fieldItem.add((riskBean.getRskState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(riskBean.getRskDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(riskBean.getRskDate(), DateUtil.TIME));
			fieldItem.add(riskBean.getRskRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IRiskService getRiskService() {
		return riskService;
	}

	public void setRiskService(IRiskService riskService) {
		this.riskService = riskService;
	}

	public TRisk getRiskBean() {
		return riskBean;
	}

	public void setRiskBean(TRisk riskBean) {
		this.riskBean = riskBean;
	}

	public List<TRisk> getRiskList() {
		return riskList;
	}

	public void setRiskList(List<TRisk> riskList) {
		this.riskList = riskList;
	}

	public String getRskNo() {
		return rskNo;
	}

	public void setRskNo(String rskNo) {
		this.rskNo = rskNo;
	}

	public String getRskNos() {
		return rskNos;
	}

	public void setRskNos(String rskNos) {
		this.rskNos = rskNos;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getRskTarget() {
		return rskTarget;
	}

	public void setRskTarget(String rskTarget) {
		this.rskTarget = rskTarget;
	}

	public String getRskAddition() {
		return rskAddition;
	}

	public void setRskAddition(String rskAddition) {
		this.rskAddition = rskAddition;
	}

	public String getRskCondition() {
		return rskCondition;
	}

	public void setRskCondition(String rskCondition) {
		this.rskCondition = rskCondition;
	}

	public String getRskScope() {
		return rskScope;
	}

	public void setRskScope(String rskScope) {
		this.rskScope = rskScope;
	}

	public String getRskPay() {
		return rskPay;
	}

	public void setRskPay(String rskPay) {
		this.rskPay = rskPay;
	}

	public Double getRskDay() {
		return rskDay;
	}

	public void setRskDay(Double rskDay) {
		this.rskDay = rskDay;
	}

	public Double getRskCount() {
		return rskCount;
	}

	public void setRskCount(Double rskCount) {
		this.rskCount = rskCount;
	}

	public String getRskPolicy() {
		return rskPolicy;
	}

	public void setRskPolicy(String rskPolicy) {
		this.rskPolicy = rskPolicy;
	}

	public String getRskCode() {
		return rskCode;
	}

	public void setRskCode(String rskCode) {
		this.rskCode = rskCode;
	}

	public String getRskMessage() {
		return rskMessage;
	}

	public void setRskMessage(String rskMessage) {
		this.rskMessage = rskMessage;
	}

	public String getRskState() {
		return rskState;
	}

	public void setRskState(String rskState) {
		this.rskState = rskState;
	}

	public Date getRskDate() {
		return rskDate;
	}

	public void setRskDate(Date rskDate) {
		this.rskDate = rskDate;
	}

	public Date getRskDateTop() {
		return rskDateTop;
	}

	public void setRskDateTop(Date rskDateTop) {
		this.rskDateTop = rskDateTop;
	}

	public Date getRskDateEnd() {
		return rskDateEnd;
	}

	public void setRskDateEnd(Date rskDateEnd) {
		this.rskDateEnd = rskDateEnd;
	}

	public String getRskRemark() {
		return rskRemark;
	}

	public void setRskRemark(String rskRemark) {
		this.rskRemark = rskRemark;
	}

}
